<template>
    <el-drawer
    :title="!dataForm.id ? '新增证书信息' : '修改证书信息'"
    direction="rtl"
    ref="drawer"
    size="80%"
    :visible.sync="visible">
      <div class="demo-drawer__header">
          <el-button size="mini" @click="visible = false">取 消</el-button>
          <el-button type="success" size="mini" @click="dataFormSubmit()">确定</el-button>
      </div>
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
          <el-row>
              <el-col :span="12">
                  <el-form-item label="证书录入类型">
                    <el-select v-model="dataForm.certificateType" style="width:100%">
                      <el-option
                        v-for="item in certificateTypes"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                      </el-option>
                    </el-select>
                  </el-form-item>
              </el-col>
              <el-col :span="12">
                  <el-form-item label="保管部门">
                    <el-tree-select
                      style="width:50%"
                      :data="treeData"
                      v-model="dataForm.keepDeptId"
                      node-key="id"
                      placeholder="请选择保管部门"
                      @change="keepDeptChange"
                    />
                    <el-select v-model="dataForm.keepUserId" placeholder="请选择保管人" style="width:40%">
                      <el-option
                        v-for="item in employees"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id">
                      </el-option>
                    </el-select>
                  </el-form-item>
              </el-col>
          </el-row>
          <el-row>
              <el-col :span="12">
                <el-form-item label="姓名">
                  <el-input v-model="dataForm.name" placeholder="请填写姓名"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="身份证号">
                  <el-input v-model="dataForm.idCard" placeholder="请填写身份证号"></el-input>
                </el-form-item>
              </el-col>
          </el-row>
          <el-row>
              <el-col :span="12">
                <el-form-item label="手机号">
                  <el-input v-model="dataForm.mobile" placeholder="请填写手机号"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="学历">
                  <el-select v-model="dataForm.academic" placeholder="请选择学历" style="width:100%">
                      <el-option
                        v-for="item in academics"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                      </el-option>
                    </el-select>
                </el-form-item>
              </el-col>
          </el-row>
          <el-row>
              <el-col :span="12">
                <el-form-item label="证书类型">
                  <el-select v-model="dataForm.confType" filterable placeholder="请选择证书类型" style="width:50%" @change="jobChange">
                    <el-option
                      v-for="item in jobs"
                      :key="item.id"
                      :label="item.name"
                      :value="item.code">
                    </el-option>
                  </el-select>
                  <el-select v-model="dataForm.idType" filterable placeholder="请选择证书类别" style="width:40%" @change="confChange">
                    <el-option
                      v-for="item in confInfos"
                      :key="item.id"
                      :label="item.name"
                      :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="证书专业">
                  <el-select v-model="dataForm.profession" filterable placeholder="请选择证书专业" style="width:100%">
                    <el-option
                      v-for="item in professions"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
          </el-row>
          <el-row>
              <el-col :span="12">
                <el-form-item label="证书是否注册">
                  <el-select v-model="dataForm.registerType" placeholder="请选择证书是否注册" style="width:100%">
                      <el-option
                        v-for="item in registerTypes"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                      </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="证书编号">
                  <el-input v-model="dataForm.certificateNo" placeholder="请填写证书编号"></el-input>
                </el-form-item>
              </el-col>
          </el-row>
          <el-row>
              <el-col :span="12">
                <el-form-item label="发证日期">
                  <el-date-picker type="date" v-model="dataForm.releaseTime" placeholder="选择发证日期" style="width:100%"></el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="证书到期日期">
                  <el-date-picker type="date" v-model="dataForm.validityEndTime" placeholder="选择证书到期日期" style="width:100%"></el-date-picker>
                </el-form-item>
              </el-col>
          </el-row>
          <div v-if="dataForm.certificateType == 2">
          <el-row>
              <el-col :span="12">
                <el-form-item label="借入日期">
                  <el-date-picker type="date" v-model="dataForm.borrowTime" placeholder="选择借入日期" style="width:100%"></el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="到期日期">
                  <el-date-picker type="date" v-model="dataForm.borrowEndTime" placeholder="选择到期日期" style="width:100%"></el-date-picker>
                </el-form-item>
              </el-col>
          </el-row>
          <el-row>
              <el-col :span="12">
                <el-form-item label="证书来源">
                  <el-select v-model="dataForm.borrowCandidatesSources" placeholder="请选择证书类型" style="width:100%">
                      <el-option
                        v-for="item in candidatesSources"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                      </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="来源企业名称" v-if="dataForm.borrowCandidatesSources === 2">
                  <el-input v-model="dataForm.borrowOrgName" placeholder="请填写来源企业名称"></el-input>
                </el-form-item>
              </el-col>
          </el-row>
          <el-row>
              <el-col :span="12">
                <el-form-item label="联系人">
                  <el-input v-model="dataForm.borrowContact" placeholder="请填写联系人"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="联系人手机号">
                  <el-input v-model="dataForm.borrowContactMobile" placeholder="请填写联系人手机号"></el-input>
                </el-form-item>
              </el-col>
          </el-row>
          <el-row>
              <el-col :span="24">
                <el-form-item label="上传兼职合同">
                  <el-select v-model="dataForm.uploadType" placeholder="请选择是否上传兼职合同" style="width:100%">
                      <el-option
                        v-for="item in uploadTypes"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                      </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
          </el-row>
          </div>
          <el-row>
              <el-col :span="24">
                <el-form-item label="情况说明">
                  <el-input type="textarea" :rows="6" v-model="dataForm.remark" placeholder="请填写情况说明"></el-input>
                </el-form-item>
              </el-col>
          </el-row>
          <el-row>
              <el-col :span="24">
                <el-form-item label="上传证书扫描件">
                  <el-upload
                    :action="uploadUrl()"
                    :limit="1"
                    :headers="myHeader"
                    :on-success="onCertiSuccess"
                    :on-error="onError"
                    ref="certiUpload"
                    :file-list="resultCertiInfoPicList"
                    list-type="picture-card">
                      <i class="el-icon-plus"></i>
                      <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过800kb</div>
                  </el-upload>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item v-if="dataForm.uploadType === 1" label="上传合同扫描件">
                  <el-upload
                    ref="contractUpload"
                    :action="uploadUrl()"
                    :with-credentials="true"
                    :limit="1"
                    :headers="myHeader"
                    :on-success="onContractSuccess"
                    :on-error="onError"
                    :file-list="resultContractInfoPicList"
                    list-type="picture-card">
                    <i class="el-icon-plus"></i>
                    <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过800kb</div>
                  </el-upload>
                </el-form-item>
              </el-col>
          </el-row>
          <el-form-item style="text-align:center;padding-right:120px;">
              <el-button @click="visible = false">取 消</el-button>
              <el-button type="success" @click="dataFormSubmit()">保 存</el-button>
          </el-form-item>
      </el-form>
    </el-drawer>
</template>

<script>
  import Vue from "vue"
  import ElTreeSelect from "@/components/treeSelect/index.vue"
  export default {
    data () {
      return {
        visible: false,
        certificateTypes:[{
          value: 1,
          label: '在职员工证书'
        }, {
          value: 2,
          label: '兼职证书'
        }],
        academics: [{
          value: '博士',
          label: '博士'
        }, {
          value: '硕士',
          label: '硕士'
        }, {
          value: '本科',
          label: '本科'
        }, {
          value: '专科',
          label: '专科'
        }, {
          value: '高职高专',
          label: '高职高专'
        }, {
          value: '中专',
          label: '中专'
        }, {
          value: '高中',
          label: '高中'
        }, {
          value: '初中',
          label: '初中'
        }, {
          value: '小学',
          label: '小学'
        }, {
          value: '其他',
          label: '其他'
        }],
        registerTypes:[{
          value: 0,
          label: '否'
        },{
          value: 1,
          label: '是'
        },{
          value: 2,
          label: '非注册类证书'
        }],
        candidatesSources:[{
          value: 1,
          label: '个人'
        },{
          value: 2,
          label: '企业'
        }],
        uploadTypes:[{
          value: 0,
          label: '否'
        },{
          value: 1,
          label: '是'
        }],
        dataForm: {
          certificateType: 1,
          uploadType: 0,
          keepDeptId: '',
          keepUserId: '',
          name: '',
          confType: '',
          idType: '',
          idCard: '',
          certificateNo: '',
          mobile: '',
          profession:'',
          registerType:'',
          releaseTime: '',
          validityEndTime: '',
          remark: '',
          orgName: '',
          academic: '',
          contact: '',
          contactMobile: '',
          borrowCandidatesSources: '',
          borrowOrgName: '',
          borrowContact: '',
          borrowContactMobile: '',
          borrowTime: '',
          borrowEndTime: ''
        },
        dataRule: {
          certificateType: [
            { required: true, message: '请选择证书录入类型', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '请填写员工名称', trigger: 'blur' }
          ],
          idType: [
            { required: true, message: '请选择证书类型', trigger: 'blur' }
          ],
          certificateNo: [
            { required: true, message: '请填写证书编号', trigger: 'blur' }
          ],
          idCard: [
            { required: true, len:18, message: '请填写身份证号', trigger: 'blur' }
          ],
          mobile: [
            { required: true, len:11, message: '请填写正确的手机号', trigger: 'blur' }
          ],
          releaseTime: [
            { required: true, message: '请选择发证日期', trigger: 'blur' }
          ],
          validityEndTime: [
            { required: true, message: '请选择证书到期日期', trigger: 'blur' }
          ],
          keepDeptId: [
            { required: true, message: '请选择保管单位', trigger: 'blur' }
          ],
          keepUserId: [
            { required: true, message: '请选择保管人', trigger: 'blur' }
          ],
          profession: [
            { required: true, message: '请选择专业', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '请选择备注信息不能为空', trigger: 'blur' }
          ],
          borrowTime: [
            { required: true, message: '请选择借入时间', trigger: 'blur' }
          ],
          borrowEndTime: [
            { required: true, message: '请选择到期日期', trigger: 'blur' }
          ],
          orgName: [
            { required: true, message: '请填写来源企业名称', trigger: 'blur' }
          ],
          contact: [
            { required: true, message: '请填写联系人', trigger: 'blur' }
          ],
          contactMobile: [
            { required: true, message: '请填写联系人手机号', trigger: 'blur' }
          ]
        },
        treeData: [],
        certiPicList:[],
        constractPicList:[],
        jobs: [],
        jobName:'',
        confInfos: [],
        professions: [],
        resultCertiInfoPicList: [],
        resultContractInfoPicList: [],
        employees: []
      }
    },
    components: {
      ElTreeSelect
    },
    computed: {
      myHeader(){
        return {
            "token":Vue.cookie.get('token')
        }
      }
    },
    created () {
      this.initOrgTree()
      this.initJobs()
    },
    methods: {
      init(tenantId, id){
        this.visible = true
        this.tenantId = tenantId
        this.dataForm.id = id || 0
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/certificate/certificateInfo/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$nextTick(() => {
                  this.dataForm.certificateType = data.certificateInfo.certificateType
                  this.dataForm.keepDeptId = data.certificateInfo.keepDeptId
                  this.dataForm.keepUserId = data.certificateInfo.keepUserId
                  this.dataForm.name = data.certificateInfo.name
                  this.dataForm.confType = data.certificateInfo.pidType
                  this.dataForm.idType = data.certificateInfo.idType
                  this.dataForm.idCard = data.certificateInfo.idCard
                  this.dataForm.certificateNo = data.certificateInfo.certificateNo
                  this.dataForm.mobile = data.certificateInfo.mobile
                  this.dataForm.academic = data.certificateInfo.academic
                  this.dataForm.profession = data.certificateInfo.profession
                  this.dataForm.registerType = data.certificateInfo.registerType
                  this.dataForm.releaseTime = data.certificateInfo.releaseTime.substring(0,10)
                  this.dataForm.validityEndTime = data.certificateInfo.validityEndTime.substring(0,10)
                  this.dataForm.remark = data.certificateInfo.remark
                  this.dataForm.orgName = data.certificateInfo.orgName
                  this.dataForm.contact = data.certificateInfo.contact
                  this.dataForm.contactMobile = data.certificateInfo.contactMobile
                  this.dataForm.borrowCandidatesSources = data.certificateInfo.borrowCandidatesSources
                  this.dataForm.borrowOrgName = data.certificateInfo.borrowOrgName
                  this.dataForm.borrowContact = data.certificateInfo.borrowContact
                  this.dataForm.borrowContactMobile = data.certificateInfo.borrowContactMobile
                  this.dataForm.borrowTime = data.certificateInfo.borrowTime? data.certificateInfo.borrowTime.substring(0,10):''
                  this.dataForm.borrowEndTime = data.certificateInfo.borrowEndTime? data.certificateInfo.borrowEndTime.substring(0,10):''
                  if(data.certiPicList && data.certiPicList.length > 0){
                    data.certiPicList.forEach((c) => {
                      this.resultCertiInfoPicList.push({name:c.fileName, url:c.path})
                    });
                  }
                  if(data.constratPicList && data.constratPicList.length > 0){
                    data.constratPicList.forEach((c) => {
                      this.resultContractInfoPicList.push({name:c.fileName, url:c.path})
                    });
                  }
                })
              }
            })
          }
        })
      },
      onCertiSuccess(response, file, fileList){
        this.certiPicList = fileList
      },
      onContractSuccess(response, file, fileList){
        this.constractPicList = fileList
      },
      onError(err, file, fileList){
        this.$message.error("上传失败")
      },
      uploadUrl(){
        return this.$http.adornUrl(`/certificate/certificateInfo/upload`)
      },
      initJobs() {
        this.$http({
          url: this.$http.adornUrl('/certificate/certificateInfo/getJobs'),
          method: 'get',
          params: {}
        }).then(({data}) => {
          if (data && data.length > 0) {
            this.jobs = data
          }
        })
      },
      jobChange(value){
        this.confInfos = []
        this.professions = []
        this.dataForm.idType = ''
        this.dataForm.profession = ''
        this.$http({
          url: this.$http.adornUrl('/certificate/certificateInfo/getJobsConf'),
          method: 'get',
          params: this.$http.adornParams({
                  'type': value
                })
        }).then(({data}) => {
          if (data && data.length > 0) {
            this.confInfos = data
          }
        })
      },
      confChange(value){
        this.professions = []
        this.dataForm.profession = ''
        this.$http({
          url: this.$http.adornUrl('/certificate/certificateInfo/getJobsConf'),
          method: 'get',
          params: this.$http.adornParams({
                  'type': value
                })
        }).then(({data}) => {
          if (data && data.length > 0) {
            this.professions = data
          }
        })
      },
      keepDeptChange(id){
        this.employees = []
        if(this.firstCreated){
          this.dataForm.keepUserId = ''
          this.firstCreated = false
        }
        this.$http({
          url: this.$http.adornUrl('/certificate/employeeInfo/getEmployeeInfos'),
          method: 'get',
          params: this.$http.adornParams({
                  'deptId': id
                })
        }).then(({data}) => {
          if (data && data.length > 0) {
            this.employees = data
          }
        })
      },
      initOrgTree() {
        this.$http({
          url: this.$http.adornUrl('/tenant/tenantDeptInfo/tree'),
          method: 'get',
          params: {}
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.treeData = [data.deptInfo]
          } else {
            this.treeData = []
          }
        })
      },
      dataFormSubmit () {
        let certiPic = "", constractPic = ""
        if(this.certiPicList.length > 0){
          for(let i in this.certiPicList){
            if(this.certiPicList[i].response){
              certiPic += this.certiPicList[i].response.fileName + ","
            } else {
              certiPic += this.certiPicList[i].name + ","
            }
          }
        }

        if(this.constractPicList.length > 0){
          for(let i in this.constractPicList){
            if(this.constractPicList[i].response){
              constractPic += this.constractPicList[i].response.fileName + ","
            } else {
              constractPic += this.constractPicList[i].name + ","
            }
          }
        }

        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/certificate/certificateInfo/update`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id,
                'certificateType': this.dataForm.certificateType,
                'confType': this.dataForm.confType,
                'idType': this.dataForm.idType,
                'academic': this.dataForm.academic,
                'certificateNo': this.dataForm.certificateNo,
                'releaseTime': this.$dateFormat(this.dataForm.releaseTime),
                'validityEndTime': this.$dateFormat(this.dataForm.validityEndTime),
                'keepDeptId': this.dataForm.keepDeptId,
                'keepUserId': this.dataForm.keepUserId,
                'remark': this.dataForm.remark,
                'name': this.dataForm.name,
                'registerType': this.dataForm.registerType,
                'profession': this.dataForm.profession,
                'idCard': this.dataForm.idCard,
                'orgName': this.dataForm.orgName,
                'mobile': this.dataForm.mobile,
                'contact': this.dataForm.contact,
                'contactMobile': this.dataForm.contactMobile,
                'borrowCandidatesSources': this.dataForm.borrowCandidatesSources,
                'borrowOrgName': this.dataForm.borrowOrgName,
                'borrowContact': this.dataForm.borrowContact,
                'borrowContactMobile': this.dataForm.borrowContactMobile,
                'borrowTime': this.$dateFormat(this.dataForm.borrowTime),
                'borrowEndTime': this.$dateFormat(this.dataForm.borrowEndTime),
                'certiPic': certiPic,
                'constractPic': constractPic,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>

<style lang="scss">
    .demo-drawer {
        &__content {
            display: flex;
            flex-direction: column;
            height: 100%;
            form {
                flex: 1;
            }
        }
        &__footer {
            display: flex;
            button {
                flex: 1;
            }
        }
        &__header {
            text-align:right;
            margin-bottom:20px;
            width:100%;
        }
    }

    .el-drawer__body {
        padding: 20px;
    }

    .el-drawer__body{
      overflow-x:hidden;
      overflow-x:auto;
    }

    .el-upload-dragger{
      width: 100%;
    }

    .el-input__inner{
      height: 36px;
    }

</style>
